package com.beiqian.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.beiqian.config.RabbitMQConfig;
import com.beiqian.constant.RedisConstants;
import com.beiqian.domain.dto.ChatLogs;
import com.beiqian.mapper.MessageMapper;
import com.beiqian.properties.OllamaConfigProperties;
import com.beiqian.service.IOllamaChatModelService;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author: beiqian
 * @Date: 2024-08-10 10:04
 * @Description:
 */
@Service
public class OllamaChatModelServiceImpl implements IOllamaChatModelService {
    @Resource
    private ChatLanguageModel chatLanguageModel;

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private OllamaConfigProperties ollamaConfigProperties;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public String chat(String message) {

        String generate = chatLanguageModel.generate(message);

        ChatLogs chatLog = ChatLogs.builder()
                .userId(1L)
                .chatContent(generate)
                .modelName(ollamaConfigProperties.getModelName())
                .sessionId("123")
                .build();

        String jsonString = JSONObject.toJSONString(chatLog);

        String key = RedisConstants.CacheName.CHAT_CACHE + "::" + chatLog.getUserId();
        redisTemplate.opsForList().rightPush(key, jsonString);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,RabbitMQConfig.ROUTING_KEY,jsonString);
        //messageMapper.insert(chatLog);
        return generate;
    }
}
